package com.itheima.reggie.service.impl;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-05-27
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Override
    public R<ShoppingCart> reduceOnce(ShoppingCart shoppingCart) {

        //1.判断传入的是套餐id还是菜品id,并减去number属性值一次
        if(shoppingCart.getDishId() !=null){
            //1.1传递来的参数是菜品id
            ShoppingCart shoppingCart1 = lambdaQuery()
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                    .one();
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number-1);
            shoppingCart = shoppingCart1;
        }else {
            //1.2传递来的参数是套餐id
            ShoppingCart shoppingCart2 = lambdaQuery()
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                    .one();
            Integer number = shoppingCart2.getNumber();
            shoppingCart2.setNumber(number-1);
            shoppingCart = shoppingCart2;
        }
        //2.1判断number属性值是否归0,归0则删除该条记录
        if(shoppingCart.getNumber() == 0){
            lambdaUpdate().eq(ShoppingCart::getId,shoppingCart.getId()).remove();
            return R.success(shoppingCart);
        }
        //2.2不归0则重新更新字段
        update(shoppingCart,null);
        return R.success(shoppingCart);
    }
}
